package org.example.myl.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.myl.common.Response;
import org.example.myl.common.ResponseCode;
import org.example.myl.entity.Class;
import org.example.myl.entity.vo.VoStudent;
import org.example.myl.service.ClassService;
import org.example.myl.service.StudentService;
import org.example.myl.utils.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController()
@RequestMapping("/class")
@CrossOrigin
public class ClassController {
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private ClassService  classService;
    @RequestMapping(value = "/insertclass", method = RequestMethod.POST)
    public Response insert_student(@RequestParam("name") String name,
                                   @RequestParam("grade") String grade,
                                   @RequestPart("images") MultipartFile images){
        try {
            Class c=new Class();
            String url=minioUtils.upload(images);
           c.setName(name);
           c.setGrade(grade);
            c.setImages(url);
            classService.insertclass(c);
            log.info("添加成功");
            return Response.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (Exception e) {
            log.info("添加失败");
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
}
